package com.eazyshop.business.listener;


import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 对rabbitmq中的
 * ad_update队列进行监听
 * 如果这个队列中存在消息表示对应数据库中的消息被改变
 * 需要openResty重新去数据库中获取数据
 */
@Component
public class TaadListener {

    @RabbitListener(queues = "ad_update")
    public void receiveMessage(String message){
        // 发送http请求让openResty跟新数据

        // 发送请求的url地址 http://192.168.200.128/ad_update?position=web_index_lb

        String url="http://192.168.200.128/ad_update?position="+message;
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        HttpGet httpGet = new HttpGet(url);

        // 配置信息
        RequestConfig requestConfig = RequestConfig.custom()
                // 设置连接超时时间(单位毫秒)
                .setConnectTimeout(2000)
                // 设置请求超时时间(单位毫秒)
                .setConnectionRequestTimeout(2000)
                // socket读写超时时间(单位毫秒)
                .setSocketTimeout(2000)
                // 设置是否允许重定向(默认为true)
                .setRedirectsEnabled(true).build();
        // 将上面的配置信息 运用到这个Get请求里
        httpGet.setConfig(requestConfig);
        try {
            CloseableHttpResponse execute = httpClient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(url);

    }

}
